home *** CD-ROM | disk | FTP | other *** search
/ PC Media 7 / PC MEDIA CD07.iso / share / prog / cm / cmtlist.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-06  |  3.9 KB  |  82 lines

  1. // CmTList.h
  2. // -----------------------------------------------------------------
  3. // Compendium - C++ Container Class Library
  4. // Copyright (C) 1992-1994, Glenn M. Poorman, All rights reserved
  5. // -----------------------------------------------------------------
  6. // Singly linked list template definition.
  7. // -----------------------------------------------------------------
  8.  
  9. #ifndef _CMTLIST_H
  10. #define _CMTLIST_H
  11.  
  12. #include <cm/include/cmtcont.h>
  13.  
  14. template <class T> class CmTLink;                // List link class stub.
  15. template <class T> class CmTLinkedListIterator;  // List iterator class stub.
  16.  
  17. template <class T> class CmTLinkedList : public CmTContainer<T> {
  18. public:
  19.   CmTLinkedList() : _first(NULL), _last(NULL) {} // Default list constructor.
  20.   CmTLinkedList(const CmTLinkedList<T>&);        // List copy constructor.
  21.  ~CmTLinkedList();                               // List destructor.
  22.  
  23.   CmTLinkedList<T>& operator=(const CmTLinkedList<T>&); // Assignment operator.
  24.  
  25.   const T& operator[]  (int) const;              // Get item by index.
  26.   Bool     add         (const T&);               // Add item to end of list.
  27.   Bool     append      (const T&);               // Add item to end of list.
  28.   Bool     prepend     (const T&);               // Add item to start of list.
  29.   Bool     insertAfter (const T&, const T&);     // Insert item after another.
  30.   Bool     insertBefore(const T&, const T&);     // Insert item before another.
  31.   Bool     remove      (const T&);               // Remove equal item from list.
  32.   T        removeFirst ();                       // Remove first list item.
  33.   T        removeLast  ();                       // Remove last list item.
  34.   const T& lookup      (const T&) const;         // Lookup equal item in list.
  35.   const T& first       () const;                 // Return first object in list.
  36.   const T& last        () const;                 // Return last object in list.
  37.   Bool     contains    (const T&) const;         // See if equal is in list.
  38.   unsigned occurrences (const T&) const;         // Count occurrences of item.
  39.   void     removeAll   ();                       // Remove all items from list.
  40.  
  41.   CmTIterator<T>* newIterator() const;           // Create and return iterator.
  42.  
  43. protected:
  44.   CmTLink<T> *_first;                            // First item in list.
  45.   CmTLink<T> *_last;                             // Last item in list.
  46.   friend      CmTLinkedListIterator<T>;          // Iterator can access.
  47. };
  48.  
  49.                                                  // Iterator definition.
  50. template <class T> class CmTLinkedListIterator : public CmTIterator<T> {
  51. public:
  52.   CmTLinkedListIterator(const CmTLinkedList<T>&); // Iterator constructor.
  53.  
  54.   Bool     done    () const;                     // Check if end of list.
  55.   const T& next    ();                           // Return and advance.
  56.   const T& previous();                           // Return and backup.
  57.   const T& current () const;                     // Return current.
  58.   void     first   ();                           // Move to first item.
  59.   void     last    ();                           // Move to last item.
  60.  
  61. protected:
  62.   const CmTLinkedList<T> &_list;                 // List being iterated.
  63.   CmTLink<T>             *_link;                 // Current list link.
  64.   friend                  CmTLinkedList<T>;      // List class can access.
  65. };
  66.  
  67. template <class T> class CmTLink {               // List link definition.
  68. protected:
  69.   CmTLink<T> *_next;                             // Next item in list.
  70.   T           _data;                             // List data object.
  71.   friend      CmTLinkedList<T>;                  // List class can access.
  72.   friend      CmTLinkedListIterator<T>;          // Iterator class can access.
  73.  
  74.   CmTLink(const T& D) : _next(NULL), _data(D) {} // Construct link.
  75. };
  76.  
  77. #if defined(__TURBOC__) || defined(__xlC__)
  78. #include <cm/include/cmtlist.cc>
  79. #endif
  80.  
  81. #endif
  82.